using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.Globalization;

namespace Microsoft.Office.Project.EPMSync.Config.Library
{
    class NetTraceListener : TraceListener, IDisposable
    {
        UdpClient _client; 
        IPEndPoint _endPoint;

        public NetTraceListener(string IP, int Port)
        {
            _endPoint = new IPEndPoint(IPAddress.Parse(IP), Port);
            _client = new UdpClient();
        }

        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    _client.Close();
                }
            }            
            finally
            {
                base.Dispose(disposing);
            }

        }

        private string IndentString(string Message)
        {
            return Message.PadLeft(this.IndentLevel * this.IndentSize, ' ');
        }

        public override void Write(object obj)
        {
            Write(obj.ToString());
        }

        public override void Write(string Message)
        {
            WriteOutput(Message, string.Empty, false);
        }

        public override void Write(object obj, string Category)
        {
            Write(obj.ToString(), Category);
        }

        public override void Write(string Message, string Category)
        {
            WriteOutput(Message, Category, false);
        }

        public override void WriteLine(object obj)
        {
            WriteLine(obj.ToString());
        }

        public override void WriteLine(string Message)
        {
            WriteOutput(Message, string.Empty, true);
        }

        public override void WriteLine(object obj, string Category)
        {
            WriteLine(obj.ToString(), Category);
        }

        public override void WriteLine(string Message, string Category)
        {
            WriteOutput(Message, Category, true);
        }

        private void WriteOutput(string Message, string Category, bool AddNewLine)
        {
            if (_client == null)
                return;

            string output = string.Empty;
            string newLine = string.Empty;

            if (AddNewLine)
                newLine = Environment.NewLine;

            if (String.IsNullOrEmpty(Category))
                output = String.Format(CultureInfo.InvariantCulture, "{0}{1}", Message, newLine);
            else
                output = String.Format(CultureInfo.InvariantCulture, "{0}: {1}{2}", Category, Message, newLine);

            if (this.IndentLevel > 0)
                output = IndentString(output);

            try
            {
                byte[] bytes = Encoding.ASCII.GetBytes(output);
                _client.Send(bytes, bytes.Length, _endPoint);
            }
            catch
            { }
        }


    }
}
